{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pyreadstat import pyreadstat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df, metadata = pyreadstat.read_sav(R'data\\indentity问卷数据数据清理后.sav',\n",
    "                                   apply_value_formats=True,\n",
    "                                   formats_as_ordered_category=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>年级</th>\n",
       "      <th>预科</th>\n",
       "      <th>大一</th>\n",
       "      <th>大二</th>\n",
       "      <th>大三</th>\n",
       "      <th>大四</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>政治面貌</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>团员</th>\n",
       "      <td>12</td>\n",
       "      <td>227</td>\n",
       "      <td>191</td>\n",
       "      <td>87</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>党员</th>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>27</td>\n",
       "      <td>44</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>群众</th>\n",
       "      <td>1</td>\n",
       "      <td>26</td>\n",
       "      <td>26</td>\n",
       "      <td>22</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>其他</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "年级    预科   大一   大二  大三  大四\n",
       "政治面貌                      \n",
       "团员    12  227  191  87  57\n",
       "党员     0   12   27  44  20\n",
       "群众     1   26   26  22  11\n",
       "其他     0    3    5   8   0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(df['政治面貌'],df['年级'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>年级</th>\n",
       "      <th>预科</th>\n",
       "      <th>大一</th>\n",
       "      <th>大二</th>\n",
       "      <th>大三</th>\n",
       "      <th>大四</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>政治面貌</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>团员</th>\n",
       "      <td>92.307692</td>\n",
       "      <td>84.701493</td>\n",
       "      <td>76.706827</td>\n",
       "      <td>54.037267</td>\n",
       "      <td>64.772727</td>\n",
       "      <td>73.684211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>党员</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.477612</td>\n",
       "      <td>10.843373</td>\n",
       "      <td>27.329193</td>\n",
       "      <td>22.727273</td>\n",
       "      <td>13.222080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>群众</th>\n",
       "      <td>7.692308</td>\n",
       "      <td>9.701493</td>\n",
       "      <td>10.441767</td>\n",
       "      <td>13.664596</td>\n",
       "      <td>12.500000</td>\n",
       "      <td>11.039795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>其他</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.119403</td>\n",
       "      <td>2.008032</td>\n",
       "      <td>4.968944</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.053915</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "年级           预科         大一         大二         大三         大四        All\n",
       "政治面貌                                                                  \n",
       "团员    92.307692  84.701493  76.706827  54.037267  64.772727  73.684211\n",
       "党员     0.000000   4.477612  10.843373  27.329193  22.727273  13.222080\n",
       "群众     7.692308   9.701493  10.441767  13.664596  12.500000  11.039795\n",
       "其他     0.000000   1.119403   2.008032   4.968944   0.000000   2.053915"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(df['政治面貌'],df['年级'],normalize='columns',margins=True)*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 定类与定类采用tau_y系数\n",
    "def goodmanKruska_tau_y(df, x: str, y: str) -> float:\n",
    "    '''\n",
    "    古德曼和克鲁斯卡尔 tau(Goodman and Kruskal's tau measure)\n",
    "    是一种计算两个定类变量相关程度的系数，值介于0-1之间，具有消除误差比例的意义。\n",
    "    该方法的特点是在计算系数值时，会包含所有的边缘次数和条件次数，故其敏感性高于Lambda系数。\n",
    "    如果两个变量是不对称关系，最好选用tau-y来简化两个变量的相关情况。\n",
    "\n",
    "    参考文献：\n",
    "    李沛良. (2001). 社会研究的统计应用. 社会科学文献出版社\n",
    "\n",
    "    参数：\n",
    "\n",
    "    df：DataFrame\n",
    "    x: 作为自变量的定类变量\n",
    "    y：作为因变量的定类变量\n",
    "\n",
    "    返回值：\n",
    "    tau-y系数，介于0-1之间\n",
    "    '''\n",
    "    # 取得条件次数表\n",
    "    cft = pd.crosstab(df[y], df[x], margins=True)\n",
    "    # 取得全部个案数目\n",
    "    n = cft.at['All', 'All']\n",
    "    # 初始化变量\n",
    "    E_1 = E_2 = tau_y = 0\n",
    "\n",
    "    # 计算E_1\n",
    "    for i in range(cft.shape[0] - 1):\n",
    "        F_y = cft['All'][i]\n",
    "        E_1 += ((n - F_y) * F_y) / n\n",
    "    # 计算E_2\n",
    "    for j in range(cft.shape[1] - 1):\n",
    "        for k in range(cft.shape[0] - 1):\n",
    "            F_x = cft.iloc[cft.shape[0] - 1, j]\n",
    "            f = cft.iloc[k, j]\n",
    "            E_2 += ((F_x - f) * f) / F_x\n",
    "    # 计算tauy\n",
    "    tau_y = (E_1 - E_2) / E_1\n",
    "\n",
    "    return tau_y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.005893383769062919"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goodmanKruska_tau_y(df,'政治面貌','会打多少分')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>会打多少分</th>\n",
       "      <th>零到二十</th>\n",
       "      <th>20~40</th>\n",
       "      <th>四十到六十</th>\n",
       "      <th>六十到八十</th>\n",
       "      <th>八十到一百</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>年级</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>预科</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>大一</th>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>35</td>\n",
       "      <td>137</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>大二</th>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>57</td>\n",
       "      <td>103</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>大三</th>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>34</td>\n",
       "      <td>80</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>大四</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>39</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "会打多少分  零到二十  20~40  四十到六十  六十到八十  八十到一百\n",
       "年级                                     \n",
       "预科        0      0      4      7      2\n",
       "大一        6      9     35    137     81\n",
       "大二       10     12     57    103     67\n",
       "大三        7      3     34     80     37\n",
       "大四        2      2     16     39     29"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定序与定序采用交互分类表进行简化\n",
    "pd.crosstab(df['年级'],df['会打多少分'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SomersDResult(statistic=-0.033758214134459064, pvalue=0.23637035779471138, table=array([[  0,   0,   4,   7,   2],\n",
       "       [  6,   9,  35, 137,  81],\n",
       "       [ 10,  12,  57, 103,  67],\n",
       "       [  7,   3,  34,  80,  37],\n",
       "       [  2,   2,  16,  39,  29]]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定序与定序采用somers dy系数\n",
    "from scipy.stats import somersd\n",
    "x = df['年级'].cat.codes\n",
    "y = df['会打多少分'].cat.codes\n",
    "somersd(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = df[['年级', '会打多少分']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.corr('spearman')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = pd.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)],\n",
    "                  columns=['dogs', 'cats'])\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dogs</th>\n",
       "      <th>cats</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>dogs</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.948683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cats</th>\n",
       "      <td>-0.948683</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          dogs      cats\n",
       "dogs  1.000000 -0.948683\n",
       "cats -0.948683  1.000000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.corr('spearman')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4, metadata = pyreadstat.read_sav(r'data/pearson.sav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.stats as stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = df4['教育年期']\n",
    "y = df4['家务劳动时间']\n",
    "r, p = stats.pearsonr(x, y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "9650cb4e16cdd4a8e8e2d128bf38d875813998db22a3c986335f89e0cb4d7bb2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
